<?php
class EditorWidget extends Widget {

    public $targetClass='tinymce';
    public $type;
    private $_assetsUrl;

    /**
     * Initializes the widgets
     */
    public function init() {
        parent::init();

        if ($this->_assetsUrl === null) {
            $assetsDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
            $this->_assetsUrl = Yii::app()->assetManager->publish($assetsDir);
        }
    }

    /**
     * Execute the widgets
     */
    public function run() {
        Yii::beginProfile(get_class($this));

        Yii::app()->clientScript->registerScriptFile($this->_assetsUrl . '/tinymce/tinymce.min.js', CClientScript::POS_HEAD);
        
        switch(strtolower($this->type)){
            case 'textonly':
                $this->textOnlyMode();
                break;
            case 'advance': 
                $this->advanceMode(); 
                break;
            case 'custom':
                $this->customMode();
                break;
            case 'basic': 
            default:
                $this->basicMode();
                break;
        }
        Yii::endProfile(get_class($this));
    }
    
    private function basicMode(){
        Yii::app()->clientScript->registerScript('simple_tinymce', '
            tinymce.init({
                selector: "textarea.'.$this->targetClass.'",
                plugins: [
                    "advlist autolink lists link image charmap print preview anchor",
                    "searchreplace visualblocks code fullscreen",
                    "insertdatetime media table contextmenu paste"
                ],
                toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
            });
        ', CClientScript::POS_HEAD);
    }
    
    private function textOnlyMode(){
        Yii::app()->clientScript->registerScript('simple_tinymce', '
            tinymce.init({
                selector: "textarea.'.$this->targetClass.'",
                plugins: [
                    "advlist autolink lists link charmap print preview anchor",
                    "searchreplace visualblocks code fullscreen",
                    "insertdatetime media table contextmenu paste"
                ],
                toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link"
            });
        ', CClientScript::POS_HEAD);
    }
    
    private function advanceMode(){
        Yii::app()->clientScript->registerScript('advance_tinymce', '
            tinymce.init({
                selector: "textarea.'.$this->targetClass.'",
                theme: "modern",
                plugins: [
                    "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                    "searchreplace wordcount visualblocks visualchars code fullscreen",
                    "insertdatetime media nonbreaking save table contextmenu directionality",
                    "emoticons template paste textcolor"
                ],
                toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
                toolbar2: "print preview media | forecolor backcolor emoticons",
                image_advtab: true,
                templates: [
                    {title: \'Test template 1\', content: \'Test 1\'},
                    {title: \'Test template 2\', content: \'Test 2\'}
                ]
            });
        ', CClientScript::POS_HEAD);
    }
    
    private function customMode(){
        Yii::app()->clientScript->registerScript('custom_tinymce', '
            tinymce.init({
                selector: "textarea.'.$this->targetClass.'",
                theme: "modern",
                plugins: [
                    "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                    "searchreplace wordcount visualblocks visualchars code fullscreen",
                    "insertdatetime media nonbreaking save table contextmenu directionality",
                    "emoticons template paste textcolor"
                ],
                toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
                toolbar2: "print preview media | forecolor backcolor emoticons",
                image_advtab: true,
                templates: [
                    {title: \'Test template 1\', content: \'Test 1\'},
                    {title: \'Test template 2\', content: \'Test 2\'}
                ]
            });
        ', CClientScript::POS_HEAD);
    }
    
}//end class